<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/17 0017
 * Time: 上午 11:05
 */

namespace app\admin\controller;

use app\admin\model\Product as ProductModel;
use app\admin\model\Specification;
use think\Request;

class Product extends Common
{
    /**
     * 产品列表
     */
    public function index()
    {
        $product = new ProductModel();
        $spec = new Specification();
        $list = $product::all()->toarray();
        foreach ($list as $k => $v){
            $list[$k]['spec'] = $spec->field('s_name')->where('s_pid',$v['id'])->select()->toArray();
        }
        $this->assign('list',$list);
        return $this->fetch('admin/product/index');
    }

    /**
     * @title   添加产品显示页
     * @return mixed
     */
    public function add()
    {
        return $this->fetch('admin/product/add');
    }

    /**
     * @title   修改产品显示页
     */
    public function edit(&$id=null)
    {
        $product = new ProductModel();
        $res = $product::get($id);
        $this->assign('res',$res);
        return $this->fetch('admin/product/edit');
    }

    /**
     * @title   添加或更新产品
     */
    public function update()
    {
        $post = Request::instance()->param();
        if(!isset($post['id'])){
            $data['p_number'] = rand(100000,999999);
        }else{
            $data['id'] = $post['id'];
        }
        $data['p_name'] = $post['p_name'];
        $data['p_price'] = $post['p_price'];
        $data['p_photo'] = isset($post['p_photo'])?$post['p_photo']:$post['head'];
        $data['p_type'] = isset($post['p_type'])?$post['p_type']:'2';
//        dump($data);die;
        $res = $this->store(new ProductModel(),$data);
        if($res['status']=='success'){
            $this->success($res['msg'],'index');
        }else{
            $this->error($res['mag']);
        }
    }

    /**
     * @title   删除产品
     * @param $id   产品id
     */
    public function delete(&$id = nulll)
    {
        $product = new ProductModel();
        $del = $product::get($id);
        if($del->delete()){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }

    /**
     * @title   产品规格列表
     */
    public function specList()
    {
        $spec = new Specification();
        $product = new ProductModel();
        $list = $spec->select();
        foreach ($list as $k => $v){
            $list[$k]['s_product'] = $product->where('id',$v['s_pid'])->value('p_name');
        }
        $this->assign('list',$list);
        return $this->fetch('admin/product/spec_list');
    }

    /**
     * @title   添加产品规格页
     * @return mixed
     */
    public function specAdd()
    {
        $product = new ProductModel();
        $prd_list = $product::all(['p_type'=>2])->toarray();
        $this->assign('select',$prd_list);
        return $this->fetch('admin/product/spec_add');
    }

    /**
     * @title   修改产品规格页
     * @param null $id
     * @return mixed
     */
    public function specEdit(&$id = null)
    {
        $product = new ProductModel();
        $prd_list = $product::all(['p_type'=>2])->toarray();
        $this->assign('select',$prd_list);

        $spec = new Specification();
        $res = $spec->where('id',$id)->find();
        $res['product'] = $product->field('id,p_name')->where('id',$res['s_pid'])->find();
        $this->assign('res',$res);
        return $this->fetch('admin/product/spec_edit');
    }

    /**
     * @title   添加或修改产品规格
     */
    public function specUpdate()
    {
        $data = Request::instance()->param();
        $data['create_time'] = time();
        $res = $this->store(new Specification(),$data);
        if($res['status'] = 'success'){
            $this->success($res['msg'],'specList');
        }else{
            $this->error($res['msg']);
        }
    }

    /**
     * @title   删除产品规格
     * @param null $id
     */
    public function specDelete(&$id=null)
    {
        $del = Specification::get($id);
        if($del->delete()){
            $this->success('删除完成');
        }else{
            $this->error('删除失败');
        }
    }
}